package org.ciallo.easytokencore.annotation.handler;

import org.ciallo.easytokencore.annotation.EasyTokenAutoRefresh;
import org.ciallo.easytokencore.context.EasyContextHolder;
import org.ciallo.easytokencore.context.model.EasyResponse;
import org.ciallo.easytokencore.context.model.EasyToken;

import java.lang.reflect.Method;

/**
 * Easy-Token 自动刷新注解处理器
 *
 * <p>核心功能：</p>
 * <ul>
 *   <li>处理@EasyTokenAutoRefresh注解标记的方法</li>
 *   <li>在方法执行后自动刷新Token并设置到响应头</li>
 *   <li>与EasyContextHolder配合实现无侵入式Token刷新</li>
 * </ul>
 *
 * <p>处理流程：</p>
 * <ol>
 *   <li>方法执行完成后(postHandle阶段)</li>
 *   <li>从上下文获取当前Token</li>
 *   <li>调用refreshToken()生成新Token</li>
 *   <li>将新Token设置到响应头的"Easy-Token"字段</li>
 * </ol>
 *
 * <p>使用示例：</p>
 * <pre>
 * {@code
 * @EasyTokenAutoRefresh
 * @GetMapping("/refresh")
 * public Result refresh() {
 *     // 业务逻辑
 *     return Result.success();
 * }
 * }
 * </pre>
 *
 * @Description: Easy-Token 自动刷新注解处理器
 * @Author: Jukomu
 * @Package: org.ciallo.easytokencore.annotation.handler
 * @Project: easy-token-demo
 * @Version: 1.0.0
 * @name: EasyTokenAutoRefreshHandler
 * @Date: 2025-05-17
 * @Filename: EasyTokenAutoRefreshHandler
 * @see EasyTokenAutoRefresh
 * @see EasyContextHolder
 */
public class EasyTokenAutoRefreshHandler implements EasyAnnotationHandlerInterface<EasyTokenAutoRefresh> {
    @Override
    public Class<EasyTokenAutoRefresh> getHandlerAnnotationClass() {
        return EasyTokenAutoRefresh.class;
    }

    @Override
    public void checkMethodPreHandle(EasyTokenAutoRefresh at, Method method) {
    }

    @Override
    public void checkMethodPostHandle(EasyTokenAutoRefresh at, Method method) {
        EasyResponse response = EasyContextHolder.getResponse();
        EasyToken token = EasyContextHolder.getToken();
        response.setHeader("Easy-Token", token.refreshToken());
    }

    @Override
    public void checkMethodAfterCompletion(EasyTokenAutoRefresh at, Method method) {
    }
}
